<?php
namespace src\logic;
use \classlib\core as C;
class lmoney {
	//礼物消费购买物品
	public static function gift($view, $userid, $sid, $giftid, $nums=1) {
		//判断是否已经邮箱验证过了
		$user = C\model::load('main.pl.userinfo')->set_dev($userid)->get_one($userid);
		$lang = C\lang::inst('web');
		if (!$user || !$user['emailverify']) {
			$view->assign_error(80068, $lang->txt('action', '80068'));
		}
		$studio = C\model::load('main.pl.studio')->get_one($sid);
		$gift   = C\model::load('main.inc.gift')->get_one($giftid);
		if (!$studio || !$gift) {
			$view->assign_error(80068, $lang->txt('action', '80060'));
		}
		if ($studio['userid'] == $userid) {
			$view->assign_error(80107, $lang->txt('action', '80107'));
		}
		//判断送礼条件 -财富等级限制 --守护限制
		if ($gift['reglv'] > 0 && $user['lv'] < $gift['reglv']) {
			$view->assign_error(80061, $lang->txt('action', '80061'));
		}
		if ($gift['isguard'] && !lvipsquad::get($userid, $sid)) {
			$view->assign_error(80062, $lang->txt('action', '80062'));
		}
		//取当前的直播场次
		$eid  = levent::geteid($sid, $studio['userid']);
		//判断当前用户购买礼物的余额是否充足
		$coin = $nums * $gift['coinprice'];
		$bean = $nums * $gift['beanprice'];
		$res  = lmypros::moneyexp($userid, $studio['userid'], $coin, $bean, $nums, $studio['starlv'], $sid, $eid, $giftid, 1);
		if (!$res) {
			$view->assign_error(80030, $lang->txt('action', '80030'));
		}
		return $coin;
	}
	
	//购买VIP守护资料信息
	public static function vipsquad($view, $userid, $gid, $sid, $nums, &$expire) {
		//判断是否已经邮箱验证过了
		$uorm = C\model::load('main.pl.userinfo')->set_dev($userid);
		$user = $uorm->get_one($userid);
		$lang = C\lang::inst('web');
		if (!$user || !$user['emailverify']) {
			$view->assign_error(80068, $lang->txt('action', '80068'));
		}
		$guard  = C\model::load('main.inc.guard')->get_one($gid);
		$studio = C\model::load('main.pl.studio')->get_one($sid);
		if (empty($guard) || empty($studio)) {
			$view->assign_error(80011, $lang->txt('action', '80011'));
		}
		if ($studio['userid'] == $userid) {
			$view->assign_error(80107, $lang->txt('action', '80107'));
		}
		$coin    = $nums * $guard['coinprice'];
		$bean    = $nums * $guard['beanprice'];
		$airtime = (($gid == lvipsquad::YRefType)? 365:30) * $nums * 86400;
		//不是续费的购买还是允许的，不必验证人数的是否超过限制
		if (!lvipsquad::get($userid, $sid) && lvipsquad::total($sid) >= lvipsquad::YMAXSIZE) {
			$view->assign_error(80057, $lang->txt('action', '80057'));
		}
		$res  = lmypros::moneyexp($userid, $studio['userid'], $coin, $bean, $nums, $studio['starlv'], $sid, 0, $guard['id'], 4);
		if (!$res) {
			$view->assign_error(80030, $lang->txt('action', '80030'));
		}
		$res  = lmypros::buy($userid, 4, $guard['id'], $airtime, $nums, $sid);
		$expire = $res['expire'];
		lvipsquad::set($userid, $sid);
		//判断看看是否赠送座驾道具,如果需要道具表新增一条记录
		$setting = ($guard['settings'])? array():json_decode($guard['settings'], true);
		if ($setting && isset($setting['motorid']) && $setting['motorid'] > 0) {
			$motor = lmypros::buy($userid, 1, $setting['motorid'], $airtime, $nums, $sid);
			if ($user['motorid'] < 1 && $motor) {//未穿戴座驾的时候自动穿戴上
				$uorm->save($userid, array('motorid'=>$motor['id']));
			}
		}
		//给主播发送站内信提醒
		$reftype = ($gid == lvipsquad::YRefType)? 80066:80067;
		$reftype = $lang->txt('action', $reftype);
		$msg     = $lang->txt('action', '80058', $user['nickname'], $reftype);
		$title   = $lang->txt('action', '80059');
		lnews::send($title, $msg, $studio['userid'], 2, $userid);
		return $coin;
	}
	
	public static function flush_guard($userid, $sid, $gid, $nums, $coin) {
		//刷新数据到浏览器然后停止
		if (function_exists('fastcgi_finish_request')) {
			fastcgi_finish_request();
		}
		ignore_user_abort(true);
		//重置当前用户的权限资料 判断升级，然后广播出去
		$access  = laccess::reset($userid, $sid);
		$rds     = \classlib\cache::inst('redis')->inst();
		$mcoin   = (int)levent::evtonestat($sid, $userid, $rds);
		$data  = array('userid'=>$userid, 'guard'=>$gid, 'month'=>$nums,
				'coin'=>$mcoin+$coin, 'nums'=>$nums, 'access'=>$access);
		limapi::notifystudio($sid, json_encode($data), 0, 20018);
		//经验变化判断升级等 并发送广播通知
		$studio= C\model::load('main.pl.studio')->get_one($sid);
		lv::notify($userid, $studio['userid'], $sid);
		//统计贡献排行版数据资料信息
		levent::evtstat($sid, $userid, $coin, $nums, $rds);
		ob_end_clean();
	}
	
	//刷新后台数据到im 推送前端
	public static function flush_gift($userid, $giftid, $sid, $nums, $coin) {
		//刷新数据到浏览器然后停止
		if (function_exists('fastcgi_finish_request')) {
			fastcgi_finish_request();
		}
		ignore_user_abort(true);
		//记录送礼流水信息记录
		ob_start();
		//礼物的赠送提醒信息
		$rds   = \classlib\cache::inst('redis')->inst();
		$mcoin = (int)levent::evtonestat($sid, $userid, $rds);
		$msg   = json_encode(array('gift'=>$giftid, 'num'=>$nums, 'coin'=>$mcoin+$coin, 'uin'=>$userid));
		limapi::notifystudio($sid, $msg, 0, 20015);
		//经验变化判断升级等 并发送广播通知
		$studio= C\model::load('main.pl.studio')->get_one($sid);
		lv::notify($userid, $studio['userid'], $sid);
		//统计贡献排行版数据资料信息
		levent::evtstat($sid, $userid, $coin, $nums, $rds);
		ob_end_clean();
	}
}